/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package petstore.view.action.atendimento;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorActionForm;
import petstore.view.action.*;
import petstore.business.IAtendimentoFacade;
import petstore.business.IComumFacade;
import petstore.model.Animal;
import petstore.model.Atendimento;
import petstore.model.ExameSolicitado;

/**
 *
 * @author Tom Hayden
 */
public class ProntuarioExameAction extends CustomDispatchAction {

    private IAtendimentoFacade atendimentoFacade;
    private IComumFacade comumFacade;
    
    public void setAtendimentoFacade(IAtendimentoFacade atendimentoFacade) {
        this.atendimentoFacade = atendimentoFacade;
    }

    public void setComumFacade(IComumFacade comumFacade) {
        this.comumFacade = comumFacade;
    }
    public ActionForward novo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        request.setAttribute("exameSolicitado", null);
        return mapping.findForward("mesmaPagina");
    }
    
    public ActionForward selecionarExameSolicitado(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DynaValidatorActionForm f = (DynaValidatorActionForm) form;
        request.setAttribute("exameSolicitado", this.getExameSolicitadoFromSession((Integer)f.get("id"), request));
        return mapping.findForward("mesmaPagina");
    }
    
    public ActionForward salvar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        DynaValidatorActionForm f = (DynaValidatorActionForm) form;
        ActionErrors errors = f.validate(mapping, request);

        if (errors.isEmpty()) {
            ExameSolicitado exameSolicitado = this.getExameSolicitado(f, request);
            Integer idExameSolicitado = null;
            if (exameSolicitado.getId() != null) {
                idExameSolicitado = this.atendimentoFacade.alterarExameSolicitado(exameSolicitado);
            } else {
                idExameSolicitado = this.atendimentoFacade.incluirExameSolicitado(exameSolicitado);
            }
            exameSolicitado = this.atendimentoFacade.getExameSolicitado(idExameSolicitado);
            request.setAttribute("exameSolicitado", exameSolicitado);
            request.getSession().setAttribute("examesSolicitados", this.atendimentoFacade.listarExameSolicitadoPorAnimal(exameSolicitado.getAnimal()));
            request.setAttribute("msgSucesso", "true");
        } else {
            request.setAttribute(Globals.ERROR_KEY, errors);
            return mapping.getInputForward();
        }

        return mapping.findForward("mesmaPagina");

    }

    private ExameSolicitado getExameSolicitado(DynaValidatorActionForm f, HttpServletRequest request) {
        ExameSolicitado exameSolicitado = null;
        Integer id = (Integer) f.get("id");
        if (id == null || id.intValue() == 0) {
            exameSolicitado = new ExameSolicitado();
            Animal animal = (Animal)request.getSession().getAttribute("animal");
            exameSolicitado.setAnimal(animal);
            Atendimento atendimento = (Atendimento) request.getSession().getAttribute("atendimento");
            exameSolicitado.setDataSolicitacao(atendimento.getDataHoraAtendimento());
        } else {
            exameSolicitado = this.getExameSolicitadoFromSession(id, request);
        }
        exameSolicitado.setExame(this.comumFacade.getExame((Integer)f.get("idExame")));
        return exameSolicitado;
    }

    private ExameSolicitado getExameSolicitadoFromSession(Integer idExameSolicitado, HttpServletRequest request) {
        ExameSolicitado result = null;
        List<ExameSolicitado> examesSolicitados = (List<ExameSolicitado>) request.getSession().getAttribute("examesSolicitados");
        for (ExameSolicitado exameSolicitado : examesSolicitados) {
            if (exameSolicitado.getId().equals(idExameSolicitado)) {
                result = exameSolicitado;
                break;
            }
        }
        return result;
    }

}
